
function findLocation() {
	var query = document.location.search;

	if(query) {
		query = query.replace(/^\?/, '');
		var args = query.split('&', -1);
		for(var i=0;i<args.length;i++) {
			var v = args[i].split('=');
			if("latitude" == v[0]) {
				latitude = v[1];
			} else if("longitude" == v[0]) {
				longitude = v[1];
			} else if("radius" == v[0]) {
				radius = v[1];
			}
		}
	}

	latitude = geoip_latitude();
	longitude = geoip_longitude();

	/*
	if(!latitude || !longitude) {
		var obj;
		var isIE = false;
		if (XMLHttpRequest) {
			obj = new XMLHttpRequest();
		} else {
			isIE = true;
			obj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		obj.open("GET", "http://"+document.location.hostname+"/cgi-bin/getlocation", false);
		//obj.send("<?xml version='1.0' encoding='utf-8'?>\n<location><latitude>0.0000000000</latitude><longitude>0.0000000000</longitude></location>");
		if(isIE) {
			obj.send("");
		} else {
			obj.send(null);
		}
		if(obj.status == 200) {
			var xml = obj.responseXML;
			if(xml) {
				var lat = xml.getElementsByTagName("latitude");
				var lon = xml.getElementsByTagName("longitude");
				//alert(lat[0].value + ", " + lon[0].nodeValue);
				//latitude = lat[0].nodeValue;
				latitude = elementValue(lat[0]);
				//longitude = lon[0].nodeValue;
				longitude = elementValue(lon[0]);
				//alert(latitude + ", " + longitude);
			}
		}
	}
	*/
}


function elementValue(e)
{
	var nodes = e.childNodes;
	var i=0;
	var ret="";
	for(i=0;i<nodes.length;i++) {
		switch(nodes[i].nodeType) {
			case 3:
			case 4:
				if(nodes[i].nodeValue)
					ret += nodes[i].nodeValue;
				break;
		}
	}
	return ret;
}

function fetchServers(lat, lon, rad, lim)
{
	var obj;
	var arr;
	var url = "http://"+document.location.hostname+"/cgi-bin/listservers?radius="+rad+"&latitude="+lat+"&longitude="+lon+"&limit="+lim;

	if (XMLHttpRequest) {
		obj = new XMLHttpRequest();
	} else {
		obj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	}
	//alert(url);
	obj.open("GET", url, false);
	obj.send("");
	if(obj.status == 200) {
		var xml = obj.responseXML;
		var list = xml.getElementsByTagName("server");
		arr = new Array();
		var markerarr = new Array();
		for(var l=0; l<list.length; l++) {
			//var servers = list[0].getElementsByTagName("server");
			//alert("list[l].tagName: "+list[l].tagName);
			var attrs = list[l].childNodes;
			var i;
			//alert("len: "+attrs.length);
			var server = new Object();
			for(i=0;i<attrs.length;i++) {
				if(!attrs[i].tagName) {
					continue;
				}
				var value = elementValue(attrs[i]);
				server[attrs[i].tagName] = value;
			}
			arr.push(server);
			//var marker = new GMarker(new GLatLng(server["latitude"],server["longitude"]), {title: server.name});
			//alert("Latitude: "+server["latitude"]);
			//alert("Longitude: "+server["longitude"]);
			var marker = createMarker(server);
			markerarr.push(marker);
			//mm.addMarker(marker);
			//map.addOverlay(marker);
		}
		if(mgr) {
			if(mgr.getMarkerCount(map.getZoom())>1000) {
				if(mgr.clearMarkers)
					mgr.clearMarkers();
			}
			mgr.addMarkers(markerarr, 0);
		} else {
			for(var i=0;i<markerarr.length;i++) {
				map.addOverlay(markerarr[i]);
				//serverInfo(markerarr[i]);
			}
		}
		return arr;
	}
	return undef;
}

function isSteam(server) {
	if(server.game == "hl2" || server.game == "hl") {
		return true;
	}
	return false;
}

function showServerInfo(ip, port)
{
	window.open("serverinfo.html?ip="+ip+"&port="+port, 
		'serverinfo', 
		'menubar=0, resizable=1, scrollbars=1, status=1, titlebar=1, toolbar=0, width=600, height=400', true);
	return false;
}

// http://developer.valvesoftware.com/wiki/Steam_browser_protocol
function createMarker(server) {
	var marker = new GMarker(new GLatLng(server["latitude"],server["longitude"]), {title:server["name"],clickable:true,draggable:false});
	marker.server = server;
	if(isSteam(marker.server)) {
		GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(
						"<p><strong>"+marker.server.name+"</strong></p>"+
						"<a href=\"steam://connect/"+marker.server.ip+":"+marker.server.port+"\">Join game</a><br />"
						+marker.server.game+"/"+marker.server.gametype+"<br />"
						+"<a href=\"#\" onclick=\"showServerInfo('"+marker.server.ip+"', '"+marker.server.port+"');\">More Info</a>"
					);
			});
	} else {
		GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(
						"<p><strong>"+marker.server.name+"</strong></p>"+
						+marker.server.ip+":"+marker.server.port+"<br />"
						+marker.server.game+"/"+marker.server.gametype+"<br />"
					);
			});
	}
	return marker;
}
